﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zocono.WCS.Infrastructure.ApllicationDto.Enums;
using Zocono.WCS.Infrastructure.ApllicationDto.WCSTaskDto;
using Zocono.WCS.Infrastructure.SocketCommon;

namespace Zocono.WCS.Infrastructure.Device
{
    public class DeviceSocketClient : Device
    {

        public SortedList Sokcets = new SortedList();

        public DeviceSocketClient(List<SocketClientInfoDto> socketClientInfos)
        {
            SocketClientInfos = socketClientInfos;
        }

        public List<SocketClientInfoDto> SocketClientInfos
        {
            get;
            set;
        }



        /// <summary>
        /// 初始化
        /// </summary>
        public override void DoInit()
        {
            try
            {
                //所有的Socket
                foreach (SocketClientInfoDto item in this.SocketClientInfos)
                {
                    if (!item.IsActive) continue;
                    if (item.Timer == null) item.Timer = new System.Timers.Timer();
                    item.Timer.Enabled = false;
                    item.Timer.Interval = 1000 * item.ConnectSecond;
                    item.Timer.Elapsed += new System.Timers.ElapsedEventHandler(Timer_Elapsed);

                    SocketClient client = new SocketClient(item.IP, item.Port, item);
                    client.TimeOut = item.OutTime;
                    client.TimeHeart = item.HeartSecond;

                    Sokcets.Add(item.EnumSocketDevice, client);
                    item.PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);
                }

                IDictionaryEnumerator enumerator = this.Sokcets.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    SocketClient socket = enumerator.Value as SocketClient;
                    socket.OnSocketReceive += new SocketClient.SocketReceiveDelegate(this.Socket_OnSocketReceive);
                    socket.PropertyChanged += new PropertyChangedEventHandler(socket_PropertyChanged);
                    socket.DoInit();
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }

        void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            //重连
            if (e.PropertyName == "IsConnected")
            {
                SocketClientInfoDto item = (SocketClientInfoDto)sender;

                if (!item.IsConnected)
                {
                    item.Timer.Enabled = false;
                    item.Timer.Enabled = true;

                }
                else
                {
                    item.Timer.Enabled = false;

                }

                switch (item.EnumSocketDevice)
                {
                    case EnumSocketDevice.FENRONG:
                        break;
                }
            }
            else
            {

            }
        }

        void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            System.Timers.Timer t = (System.Timers.Timer)sender;
            t.Enabled = false;
            IDictionaryEnumerator enumerator = this.Sokcets.GetEnumerator();

            SocketClientInfoDto item = null;
            while (enumerator.MoveNext())
            {
                SocketClient socket = enumerator.Value as SocketClient;
                item = (SocketClientInfoDto)socket.DeviceType;
                if (item.Timer == t)
                {
                    if (!item.IsConnected)
                        socket.DoStart();
                    break;
                }
            }
            if (!item.IsConnected)
                t.Enabled = true;
        }

        void socket_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "IsConnected")
            {
                SocketClient client = (SocketClient)sender;

                SocketClientInfoDto item = (SocketClientInfoDto)client.DeviceType;
                item.IsConnected = client.IsConnected;
                if (!item.IsConnected)
                    item.Timer.Enabled = true;
            }
        }




        /// <summary>
        /// 数据从Client传进来
        /// </summary>
        /// <param name="device"></param>
        /// <param name="socket"></param>
        /// <param name="dataB"></param>
        /// <param name="dataS"></param>
        private void Socket_OnSocketReceive(object device, System.Net.Sockets.Socket socket, byte[] dataB, string dataS)
        {
            //接收服务端数据入口
            //Logic.SocketCHandle.Instance.SocketReceive((SocketClientInfoDto)device, socket, dataB, dataS);

        }

        /// <summary>
        /// 启动
        /// </summary>
        public override void DoStart()
        {
            try
            {
                IDictionaryEnumerator enumerator = this.Sokcets.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    SocketClient socket = enumerator.Value as SocketClient;
                    socket.DoStart();

                    if (!socket.IsConnected)
                    {
                        SocketClientInfoDto item = (SocketClientInfoDto)socket.DeviceType;
                        item.Timer.Enabled = true;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// 停止
        /// </summary>
        public override void DoStop()
        {
            try
            {
                IDictionaryEnumerator enumerator = this.Sokcets.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    SocketClient socket = enumerator.Value as SocketClient;

                    SocketClientInfoDto item = (SocketClientInfoDto)socket.DeviceType;
                    item.PropertyChanged -= new PropertyChangedEventHandler(item_PropertyChanged);
                    socket.DoStop();
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// 释放
        /// </summary>
        public override void DoRelease()
        {
            try
            {
                IDictionaryEnumerator enumerator = this.Sokcets.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    SocketClient socket = enumerator.Value as SocketClient;
                    socket.DoRelease();
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// 设备类型
        /// </summary>
        public override string DeviceType
        {
            get
            {
                return "DeviceSocketClient";
            }
        }

        /// <summary>
        /// ToString
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return this.DeviceType;
        }

        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="device"></param>
        /// <param name="data"></param>
        public void SocketSend(EnumSocketDevice device, string data)
        {
            SocketClient socket = (SocketClient)Sokcets[device];
            if (socket != null && socket.IsConnected) socket.SendData(data);
        }

        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="device"></param>
        /// <param name="data"></param>
        public void SocketSend(EnumSocketDevice device, byte[] data)
        {
            SocketClient socket = (SocketClient)Sokcets[device];
            socket.SendData(data);
        }
    }
}
